/******************************************************************************
 * @File name   :      CardDefinitionDaoImpl.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Aug 11, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Aug 11, 2015 6:02:26 PM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.card.dao.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.card.dao.ICardExportDao;
import com.cap.bts.crmdp.dealer.card.domain.CardGeneration;
import com.cap.bts.crmdp.dealer.card.dto.CardExportDto;
import com.cap.bts.framework.common.dao.callback.NativeQueryBySqlJpaCallback;
import com.cap.bts.framework.common.dao.callback.QueryWithPagingJpaCallback;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Pager;
import com.cap.bts.framework.common.util.Utils;

/**
 *
 */
@Repository("cardExportDao")
public class CardExportDaoImpl extends BaseDaoImpl<CardGeneration, Long>implements ICardExportDao {

    /**
     * @Date        :      Aug 11, 2015
     * @param clazz
     */
    @Inject
    public CardExportDaoImpl(@Value("com.cap.bts.crmdp.dealer.card.domain.CardGeneration")Class<CardGeneration> clazz) {
        super(clazz);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 24, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardExportDao#getExportCardListByQueryDto(com.cap.bts.crmdp.dealer.card.dto.CardExportQueryDto)
    **/
    @Override
    public List<CardGeneration> getExportCardListByQueryDto(CardExportDto dto) throws AppException{
        String selectStr="select a from CardGeneration a left join a.cardDefinition";
        String countStr = "select count(*) from CardGeneration a";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        StringBuilder where=new StringBuilder();
        List<CardGeneration> list=null;
        where.append(" where 1=1");
        if(dto.getQueryDto()!=null && Utils.notEmpty(dto.getQueryDto().getName())){
            where.append(" and  a.cardDefinition.name like '%"+dto.getQueryDto().getName()+"%'");
        }
        if(dto.getQueryDto()!=null && dto.getQueryDto().getStartDate()!=null){          
            where.append(" and  DATE_FORMAT(a.createDate,'%Y-%m-%d') >='"+dateFormat.format(dto.getQueryDto().getStartDate())+"'");
        }
        if(dto.getQueryDto()!=null && dto.getQueryDto().getEndDate()!=null){          
            where.append(" and  DATE_FORMAT(a.createDate,'%Y-%m-%d') <='"+dateFormat.format(dto.getQueryDto().getEndDate())+"'");
        }
        String order="order by a.id desc";
        StringBuilder selectSql=new StringBuilder();
        StringBuilder countSql=new StringBuilder();
        selectSql.append(selectStr).append(where.toString()).append(order);
        countSql.append(countStr).append(where.toString());
        Pager page = dto.getPager();
        page.setPageSize(10);
        list= super.getJpaTemplate().execute(new QueryWithPagingJpaCallback<CardGeneration>(selectSql.toString(), countSql.toString(), page));
        dto.setPager(page);
        return list;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年12月2日
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardExportDao#getWechatCardListFromView()
    **/
    @Override
    public List<String> getWechatCardListFromView(String viewName) throws AppException {
        String sql = "select * from " + viewName;
        List<String> result = this.getJpaTemplate().executeFind(new NativeQueryBySqlJpaCallback<String>(sql));
        //去除为null的数据
        List<String> resultList = new ArrayList<String>() ;
        for(String i :result) {
        	if(i != null) {
        		resultList.add(i);
        	}
        }
        return resultList;
    }

}
